/*
 * Status.java
 *
 * Created on 13 ������ 2007 �., 19:13
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package protocol;

import DrawControls.icons.*;
import java.io.*;
import chatty.comm.*;
import chatty.util.ResBundle;
import chatty.modules.*;

import protocol.simp.*;

/**
 *
 * @author R.U.S.H.A.N.
 */
public class Client {
	private static String[] 	icqClientCaps;
    private static String[] 	icqClientNames;
    private static ImageList 	icqClientIcons = ImageList.createImageList("/clients-icq.png");
	
	private static String[] 	xmppClientCaps;
    private static String[] 	xmppClientNames;
	private static ImageList 	xmppClientIcons = ImageList.createImageList("/clients-xmpp.png");
	
	private static String[] 	mrimClientCaps;
    private static String[] 	mrimClientNames;
	private static ImageList 	mrimClientIcons = ImageList.createImageList("/clients-mrim.png");
	
	private String[] 			clientCaps;
	private String[] 			clientNames;
	private ImageList 			clientIcons; 
	
    static {
		Config icq_cfg = new Config().load("/clients-icq.txt");
        icqClientCaps = icq_cfg.getKeys();
        icqClientNames = icq_cfg.getValues();
		
        Config xmpp_cfg = new Config().load("/clients-xmpp.txt");
        xmppClientCaps = xmpp_cfg.getKeys();
        xmppClientNames = xmpp_cfg.getValues();
		
		Config mrim_cfg = new Config().load("/clients-mrim.txt");
        mrimClientCaps = mrim_cfg.getKeys();
        mrimClientNames = mrim_cfg.getValues();
	}
		
    public static final Client noneClient = new Client();
    public static final byte CLI_NONE = -1;
	
    private byte clientIndex = CLI_NONE;
	
    /** Creates a new instance of Client */
    private Client() {
    }
	
	static public Client createClient(Simp p, Client prev, byte[] capsArray) {
	    if (null == prev) {
            prev = noneClient;
        }
        if (null == capsArray) {
            return prev;
        }
		
		byte clientIndex = CLI_NONE;
		int protocolType = p.getType();
		String caps = StringConvertor.byteArray1251ToString(capsArray, 0, capsArray.length);
		
		Client result = (noneClient == prev) ? new Client() : prev;
		
		switch( protocolType ) {	
			case Profile.PROTOCOL_ICQ:
				result.clientCaps = icqClientCaps;
				result.clientNames = icqClientNames;
				break;
				
            case Profile.PROTOCOL_MRIM:
				result.clientCaps = mrimClientCaps;
				result.clientNames = mrimClientNames;
				result.clientIcons = mrimClientIcons;
				break;
				
            case Profile.PROTOCOL_JABBER:
            case Profile.PROTOCOL_GTALK:
            case Profile.PROTOCOL_FACEBOOK:
            case Profile.PROTOCOL_LJ:
            case Profile.PROTOCOL_YANDEX:
            case Profile.PROTOCOL_VK:
            case Profile.PROTOCOL_QIP:
				result.clientCaps = xmppClientCaps;
				result.clientNames = xmppClientNames;
				result.clientIcons = xmppClientIcons;
                break;
		}
		if ( protocolType == Profile.PROTOCOL_ICQ ) {
			for (byte capsIndex = 0; capsIndex < result.clientCaps.length; ++capsIndex) {
				if ( capsArray.equals( Util.explodeToBytes(result.clientCaps[capsIndex], ',', 16)) ) {
					clientIndex = capsIndex;
					break;
				}
			}
		} else {
			for (byte capsIndex = 0; capsIndex < result.clientCaps.length; ++capsIndex) {
				if (-1 != caps.indexOf(result.clientCaps[capsIndex])) {
					clientIndex = capsIndex;
					break;
				}
			}
		}
		//DebugLog.println("caps = " + caps);
		//DebugLog.println("clientIndex = " + clientIndex);
		
        result.clientIndex = clientIndex;
        return result;
    }
	public int getIndex() {
		return clientIndex;
	}
    public Icon getIcon() {
		if (CLI_NONE == clientIndex) {
            return null;
        }
		return clientIcons.iconAt(clientIndex);
	}
    public String getName() {
        if (CLI_NONE == clientIndex) {
            return null;
        }
        return clientNames[clientIndex];
    }
}

